﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Windows;

public class Utility
{
    #region file utils
    // 将一行字符串写入文件
    public static bool WriteStringToFile(string path, string text)
    {
        try
        {
            FileStream fs = new FileStream(path, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(ConvertUnicodeToUtf8(text));

            sw.Close();
            fs.Close();

            return true;
        }
        catch (Exception ex)
        {
            MessageBox.Show(string.Format("WriteStringToFile Exception:{0}", ex.Message));
        }
        return false;
    }

    // 将一行字符串写入文件,如果文件已经存在，则追加字符串
    public static bool AppendStringToFile(string path, string text)
    {
        try
        {
            FileStream fs = new FileStream(path, FileMode.Append);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(text);

            sw.Close();
            fs.Close();

            return true;
        }
        catch (Exception ex)
        {
            MessageBox.Show(string.Format("AppendStringToFile Exception:{0}", ex.Message));
        }
        return false;
    }

    //从文件读取一个字符串
    public static bool ReadFileString(string path, ref string retString)
    {
        try
        {
            if (!File.Exists(path))
            {
                return false;
            }
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
            StreamReader sr = new StreamReader(fs);
            retString = sr.ReadToEnd();
            sr.Close();
            fs.Close();

            return true;
        }
        catch (Exception ex)
        {
            MessageBox.Show(string.Format("ReadFileString Exception:{0}", ex.Message));
            return false;
        }
    }

    public static void CreateTargetPath(string targetPath)
    {
        targetPath = targetPath.Replace('\\', '/');
        if (Directory.Exists(targetPath) == false)
        {
            string[] subPath = targetPath.Split('/');
            string curCheckPath = "";
            int subContentSize = subPath.Length;
            for (int i = 0; i < subContentSize; i++)
            {
                curCheckPath += subPath[i] + '/';
                if (!Directory.Exists(curCheckPath))
                {
                    Directory.CreateDirectory(curCheckPath);
                }
            }
        }
    }
    public static void DeleteFile(string path)
    {
        if (!File.Exists(path))
        {
            return;
        }
        System.IO.File.Delete(path);
    }
    // 删除一个路径下所有文件
    public static void DeleteFolder(string path)
    {
        if (!Directory.Exists(path))
        {
            return;
        }

        string[] strTemp;
        //先删除该目录下的文件
        strTemp = System.IO.Directory.GetFiles(path);
        foreach (string str in strTemp)
        {
            System.IO.File.Delete(str);
        }
        //删除子目录，递归
        strTemp = System.IO.Directory.GetDirectories(path);
        foreach (string str in strTemp)
        {
            DeleteFolder(str);
        }

        System.IO.Directory.Delete(path);
    }

    // 拷贝一个路径下所有的文件，不包含子目录
    public static void CopyPathFile(string srcPath, string distPath)
    {
        if (!Directory.Exists(srcPath))
        {
            return;
        }

        Utility.CreateTargetPath(distPath);

        string[] strLocalFile = System.IO.Directory.GetFiles(srcPath);
        foreach (string curFile in strLocalFile)
        {
            System.IO.File.Copy(curFile, distPath + "/" + Path.GetFileName(curFile), true);
        }
    }

    //根据路径和后缀获取所有的文件
    public static void GetFileNameListByPathAndType(List<string> retList, string curPath, string fileEnd)
    {
        if (null == retList)
        {
            return;
        }

        string[] fileList = Directory.GetFiles(curPath);
        string[] dictionaryList = Directory.GetDirectories(curPath);

        foreach (string curFile in fileList)
        {
            if (curFile.EndsWith(fileEnd))
            {
                string curFilePath = curFile.Replace('\\', '/');
                retList.Add(Path.GetFileNameWithoutExtension(curFilePath));
            }
        }

        // 逐层目录开始遍历，获取所有的file end的文件
        foreach (string curDic in dictionaryList)
        {
            string curDicName = curDic.Replace('\\', '/');
            GetFileNameListByPathAndType(retList, curDicName, fileEnd);
        }
    }

    //根据路径和排除后缀获取所有的文件
    public static void GetFileListByPathWithoutType(List<string> retList, string curPath, string exceptFileEnd)
    {
        if (null == retList)
        {
            return;
        }

        string[] fileList = Directory.GetFiles(curPath);
        string[] dictionaryList = Directory.GetDirectories(curPath);

        foreach (string curFile in fileList)
        {
            if (!curFile.EndsWith(exceptFileEnd))
            {
                string curFilePath = curFile.Replace('\\', '/');
                retList.Add(Path.GetFileNameWithoutExtension(curFilePath));
            }
        }

        // 逐层目录开始遍历，获取所有的file end的文件
        foreach (string curDic in dictionaryList)
        {
            string curDicName = curDic.Replace('\\', '/');
            GetFileListByPathWithoutType(retList, curDicName, exceptFileEnd);
        }
    }
    #endregion
    public static string ConvertUnicodeToUtf8(string unicode)
    {
        if (string.IsNullOrEmpty(unicode))
        {
            return string.Empty;
        }
        return new Regex(@"\\u([0-9A-F]{4})", RegexOptions.IgnoreCase | RegexOptions.Compiled)
                  .Replace(unicode, p => Convert.ToChar(Convert.ToUInt16(p.Result("$1"), 16)).ToString());
    }

    public static void ProccessTortoiseProc(string strArg)
    {
        System.Diagnostics.Process p = new System.Diagnostics.Process();
        p.StartInfo.FileName = "TortoiseProc.exe";
        p.StartInfo.Arguments = strArg;
        p.Start();//启动程序
    }
}
